package org.utfpr.pruginho;

import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.texteditor.ITextEditor;

public class ContextInfo {
	private IWorkbenchWindow window;
	String txt = "";
	Object obj;
	//CompilationUnit compilationUnit;
	
	private Info inf;
	
	private String nomeClasse;
	private String nomePacote;
	private String nomeMetodo;

	
	public ContextInfo(){
		this.nomeMetodo = " - ";
		this.nomeClasse = " - ";
		this.nomePacote = " - ";
		inf = new Info();
	}
	
	public Info getInfo() {
		IEditorPart editor = PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow().getActivePage().getActiveEditor();

		
	
		
		String info = null;

		//String x = PlatformUI.getWorkbench().getActiveWorkbenchWindow().toString();
	//	System.out.println("X: " + x);

		//txt = compilationUnit.getPackageName().toString();

		if (editor instanceof ITextEditor) {
			ITextEditor textEditor = (ITextEditor) editor;
			
			//ITextSelection textSelection = (ITextSelection) textEditor.getSelectionProvider().getSelection();

			//int offset = textSelection.getOffset(); // etc.
			//textEditor.selectAndReveal(offset - 30, offset + 30);
			
//			String lines = "";
//			try {
//				lines = textEditor.getDocumentProvider()
//						.getDocument(textEditor.getEditorInput())
//						.get(offset - 30, offset + 30);
//			} catch (BadLocationException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
			
			
			//nomeMetodo = "";
			nomeClasse = removeExtensionName(textEditor.getTitle().toString());
			nomePacote = removeClassName(textEditor.getTitleToolTip());
			

			//info = "\n---------------------------";
			//InputStream is = ClassLoader.getSystemResourceAsStream(removeExtensionName(textEditor.getTitle()));
//			info += "========>"	+ obj.toString();
			//info += "========>"	+ is.toString();
			//info += ">>>>>>>>>>" + editor.getClass().getName();
			//info += "\nTexto selecionado: " + textSelection.getText();
			//info += "\nLines: " + lines;
			info = "Classe:" + nomeClasse;
			info += "\nPacote:" + nomePacote;
			//info += "\nPosição do Cursor: " + offset;
			info += "\n---------------------------\n";

			// MessageDialog.openInformation(window.getShell(), "Plugs",
			// ((ITextEditor) editor).getTitle() +"-- "
			// + offset);
			
			inf.setMethodName(nomeMetodo);
			inf.setClassName(nomeClasse);
			inf.setPackageName(nomePacote);

		}
		
		if(info == null){
			info = "";			
		}
		

		return inf;

	}

	private String removeExtensionName(String extName) {
		String name = extName.substring(0, extName.length() - 5);

		return name;
	}
	
	private String removeClassName(String pacoteName) {
		String name = pacoteName.substring(0, pacoteName.length() - (nomeClasse.length()+5));

		return name;
	}

}
